﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Easyleave.Logic.Validation
{
    class ValidationImpl:IValidation
    {
        private XmlDocument calanderDoc = new XmlDocument();
        private XmlDocument profilesDoc = new XmlDocument();

        public ValidationImpl()
        {
            Assembly assembly = typeof(ValidationImpl).Assembly;
            string ns = assembly.GetName().Name.ToString() + ".Validation";
            //calander
            Stream xmlStream = assembly.GetManifestResourceStream(ns + ".Calander.xml");
            calanderDoc.Load(xmlStream);
            //calander
            Stream xmlStream2 = assembly.GetManifestResourceStream(ns + ".LeaveProfiles.xml");
            profilesDoc.Load(xmlStream2);
        }

        public int CountLeaves(DateTime from, DateTime to)
        {
            if (from.CompareTo(to) > 0)
            {
                return 0;
            }
            else
            {
                int leaveCount = 0;
                DateTime date = new DateTime(from.Ticks);
                while (date <= to)
                {
                    if (!IsLeave(date))
                    {
                        leaveCount++;
                    }
                    date = date.AddDays(1);
                }
                return leaveCount;
            }
        }

        private bool IsLeave(DateTime date) 
        {
            string weekDay = date.DayOfWeek.ToString();
            XmlNode day1 = calanderDoc.SelectSingleNode (string.Format("/calander/weekday/holiday[@day={0}]", new string[]{weekDay}));
            if (day1 != null) return true;

            string dateStr = date.ToShortDateString().Replace("/", "");
            XmlNode day2 = calanderDoc.SelectSingleNode(string.Format("/calander/dates/holiday[@date='{0}']", new string[] { dateStr }));

            if (day2 != null) return true;

            return false;
        }

        public int CountTotalEntitlement(Guid? userId, string leaveType)
        {
            string xpath = string.Format("/validation/user[@id='{0}']/@profile", new string[] { userId.ToString().ToUpper() });
            XmlAttribute profIdAtt = (XmlAttribute)profilesDoc.SelectSingleNode(xpath);
            xpath = string.Format("/validation/profile[@name='{0}']/leavetype[@name='{1}']/@entitlement", new string[] { profIdAtt.Value, leaveType });
            XmlAttribute entitlementAtt = (XmlAttribute)profilesDoc.SelectSingleNode(xpath);
            return int.Parse(entitlementAtt.Value);

        }

        public IList<string> GetLeaveTypes(Guid? userId)
        {
            List<string> ret = new List<string>();
            XmlAttribute profIdAtt = (XmlAttribute)profilesDoc.SelectSingleNode(string.Format("/validation/user[@id='{0}']/@profile", new string[] { userId.ToString().ToUpper() }));
            XmlNodeList nodelist = profilesDoc.SelectNodes(string.Format("/validation/profile[@name='{0}']/leavetype/@name", new string[] { profIdAtt.Value }));
            foreach (XmlNode node in nodelist)
            {
                ret.Add(((XmlAttribute)node).Value);
            }
            return ret;
        }

    }
}
